package gjbsh.bangong.ds.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import gjbsh.bangong.ds.model.单位;
import gjbsh.bangong.ds.model.组织活动;
import gjbsh.bangong.ds.model.组织活动报名;
import gjbsh.bangong.ds.service.单位Service;
import gjbsh.bangong.ds.service.组织活动Service;
import gjbsh.bangong.ds.service.组织活动报名Service;
import gjbsh.support.AppGlobalConfig;
import gjbsh.support.BooleanMessage;
import gjbsh.support.JsonResult;
import gjbsh.support.LoginUser;
import gjbsh.support.StringHandle;


@RestController
@RequestMapping("zuzhihuodongbaoming")
public class 组织活动报名Controller {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	组织活动报名Service dd组织活动报名Service;
	
	@Autowired
	组织活动Service dd组织活动Service;
	
	@Autowired
	单位Service dd单位Service;
	

	// 查找指定活动的报名列表
	@RequestMapping("find_by_huodongid")
	public JsonResult<?> find_by_huodongid(@RequestParam("huodongid")String ppHuodongId,@RequestParam("deptname")String ppDeptName){
		try {
			if("".equals(ppHuodongId)) {
				return JsonResult.getErrorResult("组织活动ID不能为空");
			}
			return JsonResult.getSuccessResult(dd组织活动报名Service.findByHuodongId(ppHuodongId,ppDeptName));
		} catch (Exception e) {
		
			logger.error("组织活动报名Controller->find_by_huodongid: "+e.getMessage());
			return JsonResult.getErrorResult("查找指定活动的报名列表失败");
		}
	}
	
	// 查找指定单位的正在活动的报名情况
	@RequestMapping("find_by_deptid")
	public JsonResult<?> find_by_deptid(@RequestParam("deptid")String ppDeptId){
		try {
			if("".equals(ppDeptId)) {
				return JsonResult.getErrorResult("单位ID不能为空");
			}
			return JsonResult.getSuccessResult(dd组织活动报名Service.findByDeptId(ppDeptId));
		} catch (Exception e) {
			
			logger.error("组织活动报名Controller->find_by_deptid: "+e.getMessage());
			return JsonResult.getErrorResult("查找指定单位的正在活动的报名情况失败");
		}
	}
	
	// 查询指定的活动报名记录
	@RequestMapping("find_one")
	public JsonResult<?> find_one(@RequestParam("huodongbaomingid")String ppHuodongbaomingId){
		try {	
			if("".equals(ppHuodongbaomingId)) {
				return JsonResult.getErrorResult("组织活动报名ID不能为空");
			}
			return JsonResult.getSuccessResult(dd组织活动报名Service.findOne(ppHuodongbaomingId));
		} catch (Exception e) {
			
			logger.error("组织活动报名Controller->find_one: "+e.getMessage());
			return JsonResult.getErrorResult("查询指定的活动报名记录失败");
		}
	}
	
	// 删除指定的活动报名记录
	@RequestMapping("delete")
	public JsonResult<?> delete(@RequestParam("huodongbaomingid")String ppHuodongbaomingId){
		try {	
			
			if("".equals(ppHuodongbaomingId)) {
				return JsonResult.getErrorResult("组织活动报名ID不能为空");
			}
			
			boolean isok = dd组织活动报名Service.delete(ppHuodongbaomingId);
			
			if(isok) {
				return JsonResult.getSuccessResult("删除成功");
			}else {
				return JsonResult.getErrorResult("删除失败");
			}
			
		} catch (Exception e) {
			
			logger.error("组织活动报名Controller->delete: "+e.getMessage());
			return JsonResult.getErrorResult("删除指定的活动报名记录失败");
		}
	}

	
	// 修改
	@RequestMapping("modify")
	public JsonResult<?> modify(
			@RequestParam("huodongbaomingid")String ppHuodongbaominghId,
			@RequestParam("huodongid")String ppHuodongId,
			@RequestParam("xingming")String ppXingming,
			@RequestParam("xingbie")String ppXingbie,
			@RequestParam("minzu")String ppMinzu,
			@RequestParam("gongzuodanweimingcheng")String ppGongzuodanweimingcheng,
			@RequestParam("zhiwu")String ppZhiwu,		
			@RequestParam("zuoji")String ppZuoji,
			@RequestParam("shouji")String ppShouji,
			@RequestParam("email")String ppEmail,
			@RequestParam("baodaoriqi")String ppBaodaoriqi,		
			@RequestParam("iszhusu")String ppIsZhusu,
			@RequestParam("isdanjian")String ppIsdanjian,
			@RequestParam("lihuiriqi")String ppLihuiriqi,		
			@RequestParam("daohuishiduan")String ppDaohuishiduan,			
			@RequestParam("fapiaotaitou")String ppFapiaotaitou,
			@RequestParam("tongxundizhi")String ppTongxundizhi,
			@RequestParam("shibiehao")String ppShibiehao,
			@RequestParam("shuiwudengjidianhua")String ppShuiwudengjidianhua,
			@RequestParam("kaihuhang")String ppkaihuhang,
			@RequestParam("zhanghao")String ppZhanghao,
			@RequestParam("dizhi")String ppDizhi,
			@RequestParam("iscanjia")String ppIscanjia,
			@RequestParam("qitayaoqiu")String ppQitayaoqiu,
			HttpServletRequest request
			){
		try {
			Map<String,Object> mm组织活动报名Map= dd组织活动报名Service.findOne(ppHuodongbaominghId);
			
			if(mm组织活动报名Map == null) {
				return JsonResult.getErrorResult("该组织活动报名纪录不存在");
			}
			
			LoginUser loginUser = (LoginUser)request.getAttribute(AppGlobalConfig.REQUEST_LOGINUSER_NAME);
			
			// 查询操作单位id
			String ppCaozuoDeptId = loginUser.getDeptid();
			
			String ppDeptId =ppCaozuoDeptId ;
			
			// 博管会操作 查询单位id
			if(ppCaozuoDeptId.equals("1")) {
				单位 mm单位 = dd单位Service.findOneByDeptname(ppGongzuodanweimingcheng);
				
				if(mm单位 == null) {
					return JsonResult.getErrorResult("未查询到该单位存在");
				}else {
					ppDeptId = mm单位.get单位id();
				}
			}
			
			String pp组织活动id = mm组织活动报名Map.get("组织活动ID") == null ? "" : mm组织活动报名Map.get("组织活动ID").toString();
			
			// 如果更改了 活动就进行名额判断
			if(!pp组织活动id.equals(ppHuodongId)) {
				
				// 进行时间和名额的判断
				BooleanMessage mmIs添加BooleanMessage = dd组织活动报名Service.IsTianjai(ppHuodongId, ppDeptId);
				
				if(!mmIs添加BooleanMessage.isOk()) {
					return JsonResult.getErrorResult(mmIs添加BooleanMessage.getMessage().toString());
				}
			}
			
		
			
            BooleanMessage mmBooleanMessage = checkShuju(ppCaozuoDeptId, ppHuodongId, ppXingming, ppXingbie, ppMinzu, ppGongzuodanweimingcheng, ppZhiwu, ppZuoji, ppShouji, ppEmail, ppBaodaoriqi, ppIsZhusu, ppIsdanjian, ppLihuiriqi, ppDaohuishiduan, ppFapiaotaitou, ppTongxundizhi, ppShibiehao, ppShuiwudengjidianhua, ppkaihuhang, ppZhanghao, ppDizhi, ppIscanjia, ppQitayaoqiu);	
			if(!mmBooleanMessage.isOk()) {
				return JsonResult.getErrorResult(mmBooleanMessage.getMessage().toString());
			}
			
			SimpleDateFormat mmSimpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
			组织活动报名 mm组织活动报名 = new 组织活动报名();

			mm组织活动报名.set组织活动报名id(ppHuodongbaominghId);
			mm组织活动报名.set组织活动id(ppHuodongId);
			mm组织活动报名.setEmail(ppEmail);
			mm组织活动报名.set其他要求(ppQitayaoqiu);
		    mm组织活动报名.set到会时段(ppDaohuishiduan);
		    mm组织活动报名.set发票抬头(ppFapiaotaitou);
		    mm组织活动报名.set地址(ppDizhi);
		    mm组织活动报名.set姓名(ppXingming);
		    mm组织活动报名.set工作单位(ppGongzuodanweimingcheng);
		    mm组织活动报名.set座机(ppZuoji);
			mm组织活动报名.set开户行(ppkaihuhang);
			mm组织活动报名.set性别(ppXingbie);
			mm组织活动报名.set手机(ppShouji);
			mm组织活动报名.set报到日期(mmSimpleDateFormat.parse(ppBaodaoriqi));
			mm组织活动报名.set是否住宿(ppIsZhusu);
			mm组织活动报名.set是否参加过培训(ppIscanjia);
			mm组织活动报名.set是否要求单间(ppIsdanjian);
			mm组织活动报名.set民族(ppMinzu);
			mm组织活动报名.set离会日期(mmSimpleDateFormat.parse(ppLihuiriqi));
			mm组织活动报名.set税务登记电话(ppShuiwudengjidianhua);
			mm组织活动报名.set纳税人识别号(ppShibiehao);
			mm组织活动报名.set职务或职称(ppZhiwu);
			mm组织活动报名.set详细通讯地址(ppTongxundizhi);
			mm组织活动报名.set账号(ppZhanghao);
			
			
            boolean Isok = dd组织活动报名Service.modify(mm组织活动报名);
			
			if(Isok) {
				return JsonResult.getSuccessResult("修改成功");
			}else {
				return JsonResult.getErrorResult("修改失败");
			}
			
		} catch (Exception e) {
			
			logger.error("组织活动报名Controller->modify: "+e.getMessage());
			return JsonResult.getErrorResult("修改指定的活动报名记录失败");
		}
	}
		
  
	
    // 添加
	@RequestMapping("add")
	public JsonResult<?> add(
			@RequestParam("huodongid")String ppHuodongId,
			@RequestParam("xingming")String ppXingming,
			@RequestParam("xingbie")String ppXingbie,
			@RequestParam("minzu")String ppMinzu,
			@RequestParam("gongzuodanweimingcheng")String ppGongzuodanweimingcheng,
			@RequestParam("zhiwu")String ppZhiwu,		
			@RequestParam("zuoji")String ppZuoji,
			@RequestParam("shouji")String ppShouji,
			@RequestParam("email")String ppEmail,
			@RequestParam("baodaoriqi")String ppBaodaoriqi,		
			@RequestParam("iszhusu")String ppIsZhusu,
			@RequestParam("isdanjian")String ppIsdanjian,
			@RequestParam("lihuiriqi")String ppLihuiriqi,		
			@RequestParam("daohuishiduan")String ppDaohuishiduan,			
			@RequestParam("fapiaotaitou")String ppFapiaotaitou,
			@RequestParam("tongxundizhi")String ppTongxundizhi,
			@RequestParam("shibiehao")String ppShibiehao,
			@RequestParam("shuiwudengjidianhua")String ppShuiwudengjidianhua,
			@RequestParam("kaihuhang")String ppkaihuhang,
			@RequestParam("zhanghao")String ppZhanghao,
			@RequestParam("dizhi")String ppDizhi,
			@RequestParam("iscanjia")String ppIscanjia,
			@RequestParam("qitayaoqiu")String ppQitayaoqiu,
			HttpServletRequest request
			){
		try {
			// 获得用户id 单位id
			LoginUser loginUser = (LoginUser)request.getAttribute(AppGlobalConfig.REQUEST_LOGINUSER_NAME);
			String ppUserId = loginUser.getUserId();
			//  操作单位id
			String ppCaozuoDeptId = loginUser.getDeptid();
			
			// 单位id
			String ppDeptId =ppCaozuoDeptId ;
			
			// 博管会操作 查询单位id
			if(ppCaozuoDeptId.equals("1")) {
				单位 mm单位 = dd单位Service.findOneByDeptname(ppGongzuodanweimingcheng);
				
				if(mm单位 == null) {
					return JsonResult.getErrorResult("未查询到该单位存在");
				}else {
					ppDeptId = mm单位.get单位id();
				}
			}
			
			// 进行时间和名额的判断
			BooleanMessage mmIs添加BooleanMessage = dd组织活动报名Service.IsTianjai(ppHuodongId, ppDeptId);
			
			if(!mmIs添加BooleanMessage.isOk()) {
				return JsonResult.getErrorResult(mmIs添加BooleanMessage.getMessage().toString());
			}
			
			// 进行数据的判断
			BooleanMessage mmBooleanMessage = checkShuju(ppCaozuoDeptId, ppHuodongId, ppXingming, ppXingbie, ppMinzu, ppGongzuodanweimingcheng, ppZhiwu, ppZuoji, ppShouji, ppEmail, ppBaodaoriqi, ppIsZhusu, ppIsdanjian, ppLihuiriqi, ppDaohuishiduan, ppFapiaotaitou, ppTongxundizhi, ppShibiehao, ppShuiwudengjidianhua, ppkaihuhang, ppZhanghao, ppDizhi, ppIscanjia, ppQitayaoqiu);
			
			if(!mmBooleanMessage.isOk()) {
				return JsonResult.getErrorResult(mmBooleanMessage.getMessage().toString());
			}
			
			// 保存数据
			SimpleDateFormat mmSimpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
			组织活动报名 mm组织活动报名 = new 组织活动报名();
			mm组织活动报名.set组织活动报名id(UUID.randomUUID().toString());
			mm组织活动报名.setEmail(ppEmail);
			mm组织活动报名.set其他要求(ppQitayaoqiu);
		    mm组织活动报名.set到会时段(ppDaohuishiduan);
		    mm组织活动报名.set发票抬头(ppFapiaotaitou);
		    mm组织活动报名.set地址(ppDizhi);
		    mm组织活动报名.set姓名(ppXingming);
		    mm组织活动报名.set工作单位(ppGongzuodanweimingcheng);
		    mm组织活动报名.set座机(ppZuoji);
			mm组织活动报名.set开户行(ppkaihuhang);
			mm组织活动报名.set性别(ppXingbie);
			mm组织活动报名.set手机(ppShouji);
			mm组织活动报名.set报到日期(mmSimpleDateFormat.parse(ppBaodaoriqi));
			mm组织活动报名.set报名帐号(ppUserId);
			mm组织活动报名.set是否住宿(ppIsZhusu);
			mm组织活动报名.set是否参加过培训(ppIscanjia);
			mm组织活动报名.set是否要求单间(ppIsdanjian);
			mm组织活动报名.set民族(ppMinzu);
			mm组织活动报名.set离会日期(mmSimpleDateFormat.parse(ppLihuiriqi));
			mm组织活动报名.set税务登记电话(ppShuiwudengjidianhua);
			mm组织活动报名.set纳税人识别号(ppShibiehao);
			mm组织活动报名.set组织活动id(ppHuodongId);
			mm组织活动报名.set职务或职称(ppZhiwu);
			mm组织活动报名.set设站单位id(ppDeptId);
			mm组织活动报名.set详细通讯地址(ppTongxundizhi);
			mm组织活动报名.set账号(ppZhanghao);
			
			
            boolean Isok = dd组织活动报名Service.add(mm组织活动报名);
			
			if(Isok) {
				return JsonResult.getSuccessResult("添加成功");
			}else {
				return JsonResult.getErrorResult("添加失败");
			}
			
		} catch (Exception e) {
			
			logger.error("组织活动报名Controller->add: "+e.getMessage());
			return JsonResult.getErrorResult("添加指定的活动报名记录失败");
		}
	}
	
	// 导出指定的活动报名记录
	@RequestMapping("exportbaomingbyhuodongid")
	public void exportbaomingbyhuodongid(@RequestParam("huodongid")String ppHuodongId,@RequestParam("deptname")String ppDeptName,HttpServletRequest request,HttpServletResponse response){
		try {		
			
			dd组织活动报名Service.exportBaomingList(ppHuodongId, ppDeptName, request, response);		
			
		} catch (Exception e) {
			logger.error("组织活动报名Controller->exportbaomingbyhuodongid: "+e.getMessage());
		}
	}
	
	// 检查数据合法性
	private BooleanMessage checkShuju(
			String ppCaozuoDanweiId,
			String ppHuodongId,
			String ppXingming,
			String ppXingbie,
			String ppMinzu,
			String ppGongzuodanweimingcheng,
			String ppZhiwu,		
			String ppZuoji,
			String ppShouji,
			String ppEmail,
			String ppBaodaoriqi,		
			String ppIsZhusu,
			String ppIsdanjian,
			String ppLihuiriqi,		
			String ppDaohuishiduan,			
			String ppFapiaotaitou,
			String ppTongxundizhi,
			String ppShibiehao,
			String ppShuiwudengjidianhua,
			String ppkaihuhang,
			String ppZhanghao,
			String ppDizhi,
			String ppIscanjia,
			String ppQitayaoqiu) throws ParseException {
		
		ppHuodongId = ppHuodongId.trim();
		组织活动 mm组织活动 =dd组织活动Service.findOne(ppHuodongId);
		if(mm组织活动 == null) {
			return BooleanMessage.getErrorMessage("该组织活动不存在");
		}
		ppXingming = ppXingming.trim();
		if("".equals(ppXingming)) {
			return BooleanMessage.getErrorMessage("姓名不能为空");
		}
		if(StringHandle.getStringByteLength(ppXingming) > 50) {
			return BooleanMessage.getErrorMessage("姓名不可超过50字节");
		}
		if("".equals(ppXingbie)) {
			return BooleanMessage.getErrorMessage("性别不能为空");
		}
		if("".equals(ppMinzu)) {
			return BooleanMessage.getErrorMessage("民族不能为空");
		}
		if("".equals(ppGongzuodanweimingcheng)) {
			return BooleanMessage.getErrorMessage("工作单位不能为空");
		}
		
		if(StringHandle.getStringByteLength(ppGongzuodanweimingcheng) > 255) {
			return BooleanMessage.getErrorMessage("工作单位不可超过255字节");
		}
		
		if("".equals(ppZhiwu)) {
			return BooleanMessage.getErrorMessage("职务不能为空");
		}
		
		if(StringHandle.getStringByteLength(ppZhiwu) > 255) {
			return BooleanMessage.getErrorMessage("职务不可超过255字节");
		}
		
		if(StringHandle.getStringByteLength(ppZuoji) > 50) {
			return BooleanMessage.getErrorMessage("座机不可超过50字节");
		}
		
		if("".equals(ppShouji)) {
			return BooleanMessage.getErrorMessage("手机号码不能为空");
		}
		if(StringHandle.getStringByteLength(ppShouji) > 50) {
			return BooleanMessage.getErrorMessage("手机号码不可超过50字节");
		}
		
		if("".equals(ppEmail)) {
			return BooleanMessage.getErrorMessage("邮箱不能为空");
		}
		if(StringHandle.getStringByteLength(ppEmail) > 50) {
			return BooleanMessage.getErrorMessage("邮箱不可超过50字节");
		}
		
		if("".equals(ppIsZhusu)) {
			return BooleanMessage.getErrorMessage("是否住宿不能为空");
		}
				
		if("".equals(ppIscanjia)) {
			return BooleanMessage.getErrorMessage("是否参加过培训不能为空");
		}
		
		
		if(StringHandle.getStringByteLength(ppQitayaoqiu) > 2000) {
			return BooleanMessage.getErrorMessage("其他要求不可超过2000字节");
		}
		// 博管会进行以下判断
		if(ppCaozuoDanweiId.equals("1")) {
			if("".equals(ppFapiaotaitou)) {
				return BooleanMessage.getErrorMessage("发票抬头不能为空");
			}
			if("".equals(ppTongxundizhi)) {
				return BooleanMessage.getErrorMessage("发票回邮地址不能为空");
			}
			if("".equals(ppShibiehao)) {
				return BooleanMessage.getErrorMessage("纳税人识别号不能为空");
			}
			if("".equals(ppShuiwudengjidianhua)) {
				return BooleanMessage.getErrorMessage("税务登记电话不能为空");
			}
			if("".equals(ppkaihuhang)) {
				return BooleanMessage.getErrorMessage("开户行不能为空");
			}
			if("".equals(ppZhanghao)) {
				return BooleanMessage.getErrorMessage("账号不能为空");
			}

		}
	
		if(StringHandle.getStringByteLength(ppFapiaotaitou) > 200) {
			return BooleanMessage.getErrorMessage("发票抬头不可超过200字节");
		}
		
		if(StringHandle.getStringByteLength(ppTongxundizhi) > 2000) {
			return BooleanMessage.getErrorMessage("详细通讯地址不可超过2000字节");
		}
		
		
		if(StringHandle.getStringByteLength(ppShibiehao) > 100) {
			return BooleanMessage.getErrorMessage("纳税人识别号不可超过100字节");
		}
		
		if(StringHandle.getStringByteLength(ppShuiwudengjidianhua) > 100) {
			return BooleanMessage.getErrorMessage("税务登记电话不可超过100字节");
		}
		
		if(StringHandle.getStringByteLength(ppkaihuhang) > 100) {
			return BooleanMessage.getErrorMessage("开户行不可超过100字节");
		}
		
		if(StringHandle.getStringByteLength(ppZhanghao) > 100) {
			return BooleanMessage.getErrorMessage("账号不可超过100字节");
		}
		
		if(StringHandle.getStringByteLength(ppDizhi) > 200) {
			return BooleanMessage.getErrorMessage("地址不可超过200字节");
		}
		

		
		return BooleanMessage.getSuccessMessage("数据合法");
	}

}
